package com.wpf.system.aspectj;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wpf.system.entity.Equipment;
import com.wpf.system.entity.EquipmentBuy;
import com.wpf.system.entity.EquipmentRent;
import com.wpf.system.entity.Orders;
import com.wpf.system.mapper.FieldBookMapper;
import com.wpf.system.service.*;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Aspect
@Component
public class CheckTimeOutAspectj {

    @Resource
    private FieldBookMapper fieldBookMapper;

    @Resource
    private EquipmentService equipmentService;

    @Resource
    private EquipmentRentService equipmentRentService;

    @Resource
    private EquipmentBuyService equipmentBuyService;

    @Resource
    private BalanceJournalService balanceJournalService;

    @Resource
    private PointJournalService pointJournalService;

    @Resource
    private OrdersService ordersService;

    @Before("execution(public * com.wpf.system.controller.FieldBookController.getAllFieldBook(..))")
    public void beforeFieldBookTimeOutAdvice() {
        fieldBookMapper.checkTimeOutFieldBook();
    }

    @Before("execution(public * com.wpf.system.controller.EquipmentRentController.getAllEquipmentRent(..))")
    public void beforeEquipmentRentTimeOutAdvice() {
        //检查已下单未领取用户且超过 领取时间+10min 的租赁记录，标记为8 管理员取消 退还全部金额 一半积分
        Date OutDateTime = DateUtil.offsetMinute(DateUtil.parse(DateUtil.now()),-10);
        LambdaQueryWrapper<EquipmentRent> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(EquipmentRent::getStatus,1);
        queryWrapper1.le(EquipmentRent::getGetTime,OutDateTime);
        List<EquipmentRent> equipmentRentNotGetList = equipmentRentService.list(queryWrapper1);
        if(equipmentRentNotGetList.size()>0){
            for( EquipmentRent item : equipmentRentNotGetList ){
                item.setStatus(8);
                equipmentRentService.getBaseMapper().updateById(item);
                //退还库存
                Equipment equipment = equipmentService.getById(item.getEid());
                equipment.setNumber(equipment.getNumber()+item.getNumber());
                equipmentService.updateById(equipment);

                Orders orders = ordersService.getById(item.getOid());
                if(orders.getPoint()>0 || orders.getCost().compareTo(BigDecimal.valueOf(0))==1){
                    if(orders.getPoint()>0){
                        //退还积分
                        pointJournalService.pointInto(orders,1);
                    }
                    if(orders.getCost().compareTo(BigDecimal.valueOf(0))==1){
                        //退还金额
                        balanceJournalService.balanceInto(orders,1,item.getId());
                    }
                }
            }
        }

        //检查已领取，但超过应归还时间，标记为6 用户超时未归还
        Date nowDateTime = DateUtil.parse(DateUtil.now());
        LambdaQueryWrapper<EquipmentRent> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(EquipmentRent::getStatus,2);
        queryWrapper2.lt(EquipmentRent::getBackTime,nowDateTime);
        List<EquipmentRent> equipmentRentNotBackList = equipmentRentService.getBaseMapper().selectList(queryWrapper2);
        if(equipmentRentNotBackList.size()>0){
            for (EquipmentRent item : equipmentRentNotBackList){
                item.setStatus(6);
                equipmentRentService.getBaseMapper().updateById(item);
            }
        }

    }

    @Before("execution(public * com.wpf.system.controller.EquipmentBuyController.getAllEquipmentBuy(..))")
    public void beforeEquipmentBuyTimeOutAdvice(){
        //检查已购买并领取器材的购买信息，若领取时间标记为2时的updateTime距离当前时间超7天，则标记为6，不可再退货
        Date OutDateTime = DateUtil.offsetDay(DateUtil.parse(DateUtil.now()),-7);
        LambdaQueryWrapper<EquipmentBuy> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(EquipmentBuy::getStatus,2);
        queryWrapper.le(EquipmentBuy::getUpdateTime,OutDateTime);
        List<EquipmentBuy> equipmentBuyList = equipmentBuyService.list(queryWrapper);
        if(equipmentBuyList.size()>0){
            for(EquipmentBuy item : equipmentBuyList){
                item.setStatus(6);
                equipmentBuyService.getBaseMapper().updateById(item);
            }
        }
    }
}
